home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / aterminfo.lha / lib_vidattr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  4.1 KB  |  167 lines

  1.  
  2. /* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for   *
  3. *  details. If they are missing then this copy is in violation of    *
  4. *  the copyright conditions.                                        */
  5.  
  6. /*
  7.  *    vidputs(newmode, outc)
  8.  *
  9.  *    newmode is taken to be the logical 'or' of the symbols in curses.h
  10.  *    representing graphic renditions.  The teminal is set to be in all of
  11.  *    the given modes, if possible.
  12.  *
  13.  *    if set-attributes exists
  14.  *        use it to set exactly what you want
  15.  *    else
  16.  *        if exit-attribute-mode exists
  17.  *            turn off everything
  18.  *        else
  19.  *            turn off those which can be turned off and aren't in
  20.  *            newmode.
  21.  *        turn on each mode which should be on and isn't, one by one
  22.  *
  23.  *    NOTE that this algorithm won't achieve the desired mix of attributes
  24.  *    in some cases, but those are probably just those cases in which it is
  25.  *    actually impossible, anyway, so...
  26.  *
  27.  */
  28.  
  29. #include "term.h"
  30.  
  31. static void do_color(int pair, int  (*outc)())
  32. {
  33. short fg, bg;
  34.  
  35.     if ( pair == 0 ) {
  36.         tputs(orig_pair, 1, outc);
  37.     } else {
  38.         fg = color_pairs[pair] & 0x0f;
  39.         bg = (color_pairs[pair] & 0xf0) >> 4;
  40.  
  41. #if TRACE
  42.     if (_tracing)
  43.     _tracef("setting colors: pair = %d, fg = %d, bg = %d\n", pair, fg, bg);
  44. #endif
  45.         tputs(tparm(set_foreground, fg), 1, outc);
  46.         tputs(tparm(set_background, bg), 1, outc);
  47.     }
  48. }
  49.  
  50. extern int _coloron;
  51.  
  52. static int current_pair = 0;
  53.  
  54. int vidputs(chtype newmode, int  (*outc)())
  55. {
  56. static chtype    previous_attr = 0;
  57. chtype        turn_off, turn_on;
  58.  
  59. #ifdef TRACE
  60.     if (_tracing)
  61.         _tracef("vidputs(%x) called", newmode);
  62. #endif
  63.  
  64.     if (set_attributes) {
  65.         tputs(tparm(set_attributes,
  66.             (newmode & A_STANDOUT) != 0,
  67.             (newmode & A_UNDERLINE) != 0,
  68.             (newmode & A_REVERSE) != 0,
  69.             (newmode & A_BLINK) != 0,
  70.             (newmode & A_DIM) != 0,
  71.             (newmode & A_BOLD) != 0,
  72.             (newmode & A_INVIS) != 0,
  73.             (newmode & A_PROTECT) != 0,
  74.             (newmode & A_ALTCHARSET) != 0), 1, outc);
  75.     } else {
  76.          if (exit_attribute_mode) {
  77.              if((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
  78.                  tputs(exit_alt_charset_mode, 1, outc);
  79.                  previous_attr &= ~A_ALTCHARSET;
  80.              }
  81.              if (previous_attr) {
  82.                  tputs(exit_attribute_mode, 1, outc);
  83.                  previous_attr = 0;
  84.              }
  85.          } else {
  86.             turn_off = ~newmode & previous_attr;
  87.  
  88.             if ((turn_off & A_ALTCHARSET) && exit_alt_charset_mode)
  89.                 tputs(exit_alt_charset_mode, 1, outc);
  90.  
  91.             if ((turn_off & A_BOLD)  &&  exit_standout_mode)
  92.                 tputs(exit_standout_mode, 1, outc);
  93.  
  94.             if ((turn_off & A_BLINK)  && exit_standout_mode)
  95.                 tputs(exit_standout_mode, 1, outc);
  96.  
  97.             if ((turn_off & A_INVIS)  && exit_standout_mode)
  98.                 tputs(exit_standout_mode, 1, outc);
  99.  
  100.             if ((turn_off & A_PROTECT)  && exit_standout_mode)
  101.                 tputs(exit_standout_mode, 1, outc);
  102.  
  103.             if ((turn_off & A_UNDERLINE)  &&  exit_underline_mode)
  104.                 tputs(exit_underline_mode, 1, outc);
  105.  
  106.                 if ((turn_off & A_REVERSE)  &&  exit_standout_mode)
  107.                 tputs(exit_standout_mode, 1, outc);
  108.  
  109.             if ((turn_off & A_STANDOUT)  &&  exit_standout_mode)
  110.                 tputs(exit_standout_mode, 1, outc);
  111.         }
  112.  
  113.         turn_on = newmode & ~previous_attr;
  114.  
  115.         if ((turn_on & A_ALTCHARSET) && enter_alt_charset_mode)
  116.         tputs(enter_alt_charset_mode, 1, outc);
  117.  
  118.         if (_coloron) {
  119.         int pair = PAIR_NUMBER(newmode);
  120.  
  121.             if (pair != current_pair) {
  122.                 current_pair = pair;
  123.                 do_color(pair, outc);
  124.             }
  125.         }
  126.  
  127.         if ((turn_on & A_BLINK)  &&  enter_blink_mode)
  128.         tputs(enter_blink_mode, 1, outc);
  129.  
  130.         if ((turn_on & A_BOLD)  &&  enter_bold_mode)
  131.         tputs(enter_bold_mode, 1, outc);
  132.  
  133.         if ((turn_on & A_DIM)  &&  enter_dim_mode)
  134.         tputs(enter_dim_mode, 1, outc);
  135.  
  136.         if ((turn_on & A_REVERSE)  &&  enter_reverse_mode)
  137.         tputs(enter_reverse_mode, 1, outc);
  138.  
  139.         if ((turn_on & A_STANDOUT)  &&  enter_standout_mode)
  140.         tputs(enter_standout_mode, 1, outc);
  141.  
  142.         if ((turn_on & A_UNDERLINE)  &&  enter_underline_mode)
  143.         tputs(enter_underline_mode, 1, outc);
  144.  
  145.     }
  146.  
  147.     previous_attr = newmode;
  148.  
  149. #ifdef TRACE
  150.     if (_tracing)
  151.         _tracef("vidputs finished");
  152. #endif
  153.     return OK;
  154. }
  155.  
  156. int vidattr(chtype newmode)
  157. {
  158.  
  159. #ifdef TRACE
  160.     if (_tracing)
  161.         _tracef("vidattr(%x) called", newmode);
  162. #endif
  163.  
  164.     return(vidputs(newmode, putchar));
  165. }
  166.  
  167.